﻿//-----------------------------------------------------------------------
// <copyright file="EntityCollection.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Entity
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using TileTool.Scene.Entity;

    /// <summary>
    /// Collection of entities.
    /// </summary>
    public class EntityCollection : IEntityCollection
    {
        #region Fields - Private

        /// <summary>
        /// List which contains the entities in this collection.
        /// </summary>
        private List<IEntity> members = new List<IEntity>();

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public EntityCollection()
        {
        }

        /// <summary>
        /// Create a new collection from another by copying the items.
        /// </summary>
        /// <param name="collection">Source collection.</param>
        public EntityCollection(IEnumerable<IEntity> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection", "Input collection must be non-null.");
            }

            foreach (IEntity entity in collection)
            {
                this.Add(entity);
            }
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets if this entity collection is read-only.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the count of entities in the collection.
        /// </summary>
        public int Count
        {
            get
            {
                return this.members.Count;
            }
        }

        /// <summary>
        /// Gets an entity at the passed index.
        /// </summary>
        /// <param name="index">Index to get the entity at.</param>
        /// <returns>Returns the entity at the passed index.</returns>
        public IEntity this[int index]
        {
            get
            {
                if ((index < 0) || (index >= this.Count))
                {
                    throw new ArgumentException(
                        "Invalid index " + index + " in range 0 to " + this.Count,
                        "index");
                }

                return this.members[index];
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Clear all entities from the collection.
        /// </summary>
        public void Clear()
        {
            this.members.Clear();
        }

        /// <summary>
        /// <para>Add the passed entity to the collection.</para>
        /// <para>If the entity is null or a duplicate, it will not be added.</para>
        /// </summary>
        /// <param name="entity">Entity to add to the collection.</param>
        /// <returns>Returns true if the entity was added.</returns>
        public bool Add(IEntity entity)
        {
            if (entity == null)
            {
                return false;
            }

            int index = this.FindIndex(entity);
            if (index != -1)
            {
                return false;
            }

            this.members.Add(entity);

            return true;
        }

        /// <summary>
        /// Add the passed enumerable of entities to the collection.
        /// 
        /// If there are null or a duplicate entities, they will not be added.
        /// </summary>
        /// <param name="entities">Entities to add to the collection.</param>
        public void Add(IEnumerable<IEntity> entities)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities", "Entities must be non-null.");
            }

            foreach (IEntity entity in entities)
            {
                this.Add(entity);
            }
        }

        /// <summary>
        /// <para>Remove the passed entity from the collection.</para>
        /// <para>If the entity exists, will return true and false if it does not.</para>
        /// </summary>
        /// <param name="entity">Entity to remove from the collection.</param>
        /// <returns>Returns true if the entity was removed.</returns>
        public bool Remove(IEntity entity)
        {
            if (entity == null)
            {
                return false;
            }

            int index = this.FindIndex(entity);
            if (index == -1)
            {
                return false;
            }

            this.members.RemoveAt(index);

            return true;
        }

        /// <summary>
        /// Tests the collection to see if an entity is in it.
        /// </summary>
        /// <param name="entity">Entity to test for in the set.</param>
        /// <returns>Returns true if the entity was found in the collection.</returns>
        public bool Contains(IEntity entity)
        {
            return this.FindIndex(entity) != -1;
        }

        /// <summary>
        /// Find the index of the passed entity.
        /// </summary>
        /// <param name="entity">Entity to find the index of.</param>
        /// <returns>Returns the index of the entity or -1 if it was not found.</returns>
        public int IndexOf(IEntity entity)
        {
            return this.FindIndex(entity);
        }

        /// <summary>
        /// Create a read-only wrapper of this collection that prevents modifications.
        /// This is an O(1) operation and should simply wrap the collection, not copy its
        /// contents.
        /// </summary>
        /// <returns>Returns a read-only wrapper of this collection.</returns>
        public IEntityCollection AsReadOnly()
        {
            return new ReadOnlyEntityCollection(this);
        }

        /// <summary>
        /// Create a copy of this collection and return it. This should not copy the objects
        /// in the collection, just the collection itself.
        /// </summary>
        /// <returns>Returns a clone of the current collection.</returns>
        public IEntityCollection Clone()
        {
            EntityCollection clone = new EntityCollection();

            foreach (IEntity entity in this.members)
            {
                clone.Add(entity);
            }

            return clone;
        }

        #endregion

        #region Methods - Public - IEnumerable

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>A System.Collections.Generic.IEnumerator<T> that can be used to
        /// iterate through the collection.</returns>
        public IEnumerator<IEntity> GetEnumerator()
        {
            return this.members.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>A System.Collections.Generic.IEnumerator<T> that can be used to
        /// iterate through the collection.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.members.GetEnumerator();
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Given an entity, find its index in our members set if it has one.
        /// </summary>
        /// <param name="entity">Entity to search for.</param>
        /// <returns>Returns the index of the object if we found it or -1 if we did not.</returns>
        private int FindIndex(IEntity entity)
        {
            return this.members.IndexOf(entity);
        }

        #endregion
    }
}
